<?php

class yzModel extends yzCore{

	protected $aProperties = array(
	   'yz.model.table'                => '',
	   'yz.model.field.id'             => '',
	   'yz.model.fields'               => '',
	   'yz.model.fields.sortable'      => '',
	   'yz.model.fields.definitions'   => '',
	   'yz.model.bindings'             => '',
	   'yz.model.default.select'       => '*',
       'yz.model.cache'                => true,
	   'yz.cache.default.lifetime'     => 7200,

	);

	public function init(){

		if ($this->getProperty('yz.model.cache')){

            $this->setObject('__cache','yzCache');

        } //if

       if (!$this->isSetObject('yzSql') && !$this->setObject('yzSql')){

            throw new yzException($this->aErrors[1001]);

            return false;

        }

        if (!$this->isSetObject('yzDatabase') && !$this->setObject('yzDatabase')){

            throw new yzException($this->aErrors[1001]);

            return false;

        }


	}

	public function find($mWhere = null, $mOrder = null, $mLimit = null){

		$sQuery = $this->yzSql->select($this->yzModelDefaultSelect,$this->yzModelTable,$mWhere,$mOrder,$mLimit);

		if ($this->yzModelCache && $this->__cache->isActualCache($this->__cache->getCacheId($sQuery))){

			return $this->__cache->getCache($this->__cache->getCacheId($sQuery));

		} // if

		$this->yzDatabase->query($sQuery);

		if ($this->yzDatabase->errorCode()!='00000'){

            return $this->yzDatabase->errorInfo();

		} else {

		    if ($this->yzModelCache){

		    	$aResult = $this->yzDatabase->fetchAll();

                $this->__cache->setCache($aResult,$sQuery);

                return $aResult;


            } // if

			return $this->yzDatabase->fetchAll();

		} // if

	} // find

	public function getItem($mId){

		$aResult = $this->find(array($this->yzModelFieldId,intval($mId)));

		if (isset($aResult[0])){

			return $aResult[0];

		} else {

			return false;

		} // if

	} // getItem

	public function getAll($mOrder = null){

        return $this->find(null, $mOrder);

	} // getAll

	public function delete($mId){

		$sQuery = $this->yzSql->delete($this->yzModelTable,array($this->yzModelFieldId,intval($mId)),1);

		$this->yzDatabase($sQuery);

		return $this->yzDatabase->errorInfo();

	} // delete

	public function insert($aData, $aValues=null){

		$sQuery = $this->yzSql->insert($this->yzModelTable,$this->filterFields($aData),$aValues);

		$this->yzDatabase->query($sQuery);

		return $this->yzDatabase->errorInfo();

	} // insert

	public function update($mId, $aData){

		$sQuery = $this->yzSql->update($this->yzModelTable,$this->filterFields($aData), array($this->yzModelFieldId=>intval($mId)));

        $this->yzDatabase->query($sQuery);

        return $this->yzDatabase->errorInfo();

	} // update

	public function change($mId, $sField, $mNewValue){

		$sQuery = $this->yzSql->update($this->yzModelTable, array($sField=>$mNewValue),array($this->yzModelFieldId=>intval($mId)));

        $this->yzDatabase->query($sQuery);

        return $this->yzDatabase->errorInfo();

	} // change

	public function save($aData, $aValues = null){

		$this->replace($this->yzModelTable,$this->filterFields($aData),$aValues);

        $this->yzDatabase->query($sQuery);

        return $this->yzDatabase->errorInfo();

	} // save

	public function filterFields($aData){

		return  array_intersect_key($aData, explode(',',$this->yzModelFields));

	}

} // class

?>